package client.util;

import empty.users.User;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

// 客户端与服务器的链接
public class ClientConn {
    public static Socket socket;
    public static OutputStream os;
    public static InputStream is;
    public static ObjectOutputStream oos;
    public static ObjectInputStream ois;

    public static User user;

    public static void connect() throws Exception {
        socket = new Socket(InetAddress.getLocalHost(),9999);
        os = socket.getOutputStream();
        is = socket.getInputStream();
        oos = new ObjectOutputStream(os);
        ois = new ObjectInputStream(is);
    }

    public void send(Object object) throws Exception{
        if(oos == null){
            oos = new ObjectOutputStream(socket.getOutputStream());
        }
        oos.writeObject(object);
    }

    public Object receive() throws Exception{
        if(ois == null){
            ois = new ObjectInputStream(socket.getInputStream());
        }


        return ois.readObject();
    }

}
